#include <iostream>
#include "../core/grayBMPLoad.h"
#include <algorithm>
#include <fstream>
#include <string>
#include <filesystem>
#include <list>
#include "../core/fileDefine.h"
#define STB_IMAGE_IMPLEMENTATION
#include "../core/stb_image.h"
#include <regex>
#include <deque>

int main()
{
  std::string forceFilePath,timeFilePath,ImageDir;
  std::cout<<"请输入图像目录路径: ";
  std::cin>>ImageDir;
  std::cout<<"正在扫描图像目录"<<std::endl;
  if(!std::filesystem::exists(ImageDir))
  {
    std::cout<<"该目录不存在,程序退出"<<std::endl;
    return -1;
  }
  std::deque<std::pair<long,std::filesystem::path>> imageList={};
  std::regex reg(R"(([0-9]+)\.bmp)");
  std::smatch match;
  for(std::filesystem::directory_entry i : std::filesystem::directory_iterator(ImageDir))
  {
      std::string fileName = i.path().filename().string();
      if(std::regex_match(fileName,match,reg) && match.size()==2)
      {
        int value = std::stoi(match[1].str());
        imageList.push_back({value,i.path()});
      }
  }
  if(imageList.size()==0)
  {
    std::cout<<"没有扫描到相应文件,程序推出"<<std::endl;
    return -1;
  }
  std::sort(imageList.begin(),imageList.end(),
      [](std::pair<long,std::filesystem::path>& p1,std::pair<long,std::filesystem::path>& p2)
        {return p1.first < p2.first;}
  );
  std::cout<<"扫描到 "<<imageList.size()<<" 份文件"<<std::endl;
  std::cout<<"请输入Time文件路径: ";
  std::cin>>timeFilePath;
  if(!std::filesystem::exists(timeFilePath))
  {
    std::cout<<"该路径无效，程序退出"<<std::endl;
    return -1;
  }
  std::cout<<"请输入载荷文件路径: ";
  std::cin>>forceFilePath;
  if(!std::filesystem::exists(forceFilePath))
  {
    std::cout<<"该路径无效，程序退出"<<std::endl;
    return -1;
  }
  std::ifstream timeFile(timeFilePath);
  std::ifstream forceFile(forceFilePath);
  if(!timeFile.is_open() || !forceFile.is_open())
  {
    std::cout<<"无法打开文件，程序退出"<<std::endl;
    return -1;
  }

  std::deque<std::pair<uint64_t,int16_t>> additionalList={};
  int timeValue,forceValue;
  while(timeFile>>timeValue && forceFile>>forceValue)
  {
    additionalList.push_back({timeValue,forceValue});
  }
  std::cout<<"已载入数据组 "<<additionalList.size()<<std::endl;
  if(additionalList.size()!=imageList.size())
  {
    std::cout<<"属性和图像数量不正确"<<std::endl;
    return -1;
  }
  std::cout<<"请输入StrmBMP文件保存路径: ";
  std::string saveFilePath;
  std::cin>>saveFilePath;
  std::ofstream saveFile(saveFilePath,std::ios::binary);
  if(!saveFile.is_open())
  {
    std::cout<<"无法创建文件"<<std::endl;
    return -1;
  }
  
  fileHead FileHead;

  saveFile.seekp(2048,std::ios::beg);
  auto imageListPtr=imageList.begin();
  auto additionalListPtr=additionalList.begin();
  uint64_t count=1;
  int x,y,n;
  for(;imageListPtr!=imageList.end();imageListPtr++,additionalListPtr++)
  {
    imageBlockHead ImageBlockHead={count++,additionalListPtr->first};
    saveFile.write((char*)&ImageBlockHead,sizeof(ImageBlockHead));
    try
    {
      BMPfile BMP(imageListPtr->second);
      ImageInfo imgInfo=BMP.GetImageInfo();
      saveFile.write(BMP.GetImageData(),imgInfo.width*imgInfo.height*imgInfo.channel);
      x=imgInfo.width;
      y=imgInfo.height;
      n=imgInfo.channel;
    }
    catch(...)
    {
      std::cerr<<"无法打开图像"<<std::endl;
      return -1;
    }
  }
  long imageLength=saveFile.tellp();
  FileHead.imageDataBlockLength=imageLength-2048;
  additionalListPtr=additionalList.begin();
  for(;additionalListPtr!=additionalList.end();additionalListPtr++)
  {
    additionalBlockHead AdditionalBlockHead={additionalListPtr->first,additionalListPtr->second};
    saveFile.write((char*)&AdditionalBlockHead,sizeof(additionalBlockHead));
  }
  FileHead.additionalBlockLength=saveFile.tellp()-imageLength;
  FileHead.imageWidth=x;
  FileHead.imageHeight=y;
  FileHead.pixelType=n;
  saveFile.seekp(0,std::ios::beg);
  saveFile.write((char*)&FileHead,sizeof(fileHead));
  std::cout<<"文件写入成功"<<std::endl;
  return 0;
}
